package top.xiaoshuaila.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import sun.plugin.liveconnect.SecurityContextHelper;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class JwtAuthencationTokenFilter extends OncePerRequestFilter {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private UserDetailsService userDetailsService;
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {
        //通过tokenHeader获取请求头里面的token
        String authHeader = request.getHeader(tokenHeader);
        //如果不为空并且是自己授予的以tokenHead开头的token,再下一步
        if(null!=authHeader&&authHeader.startsWith(tokenHead)){
            //获取正确的token
            String authToken=authHeader.substring(tokenHead.length());
            //通过jwt工具类获取token的username
            String username =
                    jwtTokenUtil.getUserNameFromToken(authToken);
            //如果获取的username不为空并且当前security没有权限,开始授权
            if (null!=username&&null== SecurityContextHolder.getContext().getAuthentication()){
                //通过username获取userdatils
                UserDetails userDetails =
                        userDetailsService.loadUserByUsername(username);
                //通过jwt工具类验证token和的当前重新或得的userdatis匹配
                if(jwtTokenUtil.validateToken(authToken,userDetails)){
                    //匹配后创建UsernamePasswordAuthenticationToken
                    UsernamePasswordAuthenticationToken authenticationToken=new
                            UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
                    authenticationToken.setDetails(new
                     WebAuthenticationDetailsSource().buildDetails(request));
                    //将UsernamePasswordAuthenticationToken对象放入
                    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
                }
            }
        }
        //通过该过滤器
        filterChain.doFilter(request,response);
    }
}
